<template>
  <div class="info">
    <h1>用户信息管理界面</h1>
    <div>
      <el-form :inline="true">
        <el-form-item style="float: right">
          <el-button type="primary" size="small" icon="el-icon-edit-outline" @click="handlerAdd()">添加</el-button>
        </el-form-item>
      </el-form>
    </div>

    <div class="table">
      <el-table
        border
        style="width: 100%" :data="tableData">
        <el-table-column
          type="index"
          label="序号"
          align="center"
          width="60">
        </el-table-column>
        <el-table-column
          label="姓名"
          align="center"
          width="100x" prop="name">
        </el-table-column>
        <el-table-column
          label="作者"
          align="center"
          width="100x" prop="author">
        </el-table-column>
        <el-table-column
          label="价格"
          align="center"
          width="100x" prop="price">
        </el-table-column>
        <el-table-column label="操作" fixed="right">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleEdit(scope.$index, scope.row)">编辑
            </el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block" style="float: right;margin-top: 15px">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="pageNum" :page-sizes="[1,10, 20, 30, 40]"
        :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="totalCount">
        </el-pagination>
      </div>
    </div>

    <AddUser :dialogAdd="dialogAdd" @update="getUserInfo"/>
    <EditUser :dialogEdit="dialogEdit" :editForm="editForm" @update="getUserInfo"/>
  </div>
</template>

<script>
  import AddUser from './AddUserInfo.vue'
  import EditUser from './EditUserInfo.vue'

  export default {
    name: 'info',
    data () {
      return {
        tableData: [],
        //默认每页数据量
        pageSize: 1,
        //当前页码
        pageNum: 1,
        //默认数据总数
        totalCount: 1000,
        editForm: {
          id: '',
          name:'',
          author:'',
          price:''
        },
        dialogAdd: {
          show: false
        },
        dialogEdit: {
          show: false
        }
      }
    },
    methods: {
      getUserInfo () {
        console.log(this.pageNum,this.pageSize)
        this.$axios.get('/api/bookpage',{params:{pageNum:this.pageNum, pageSize:this.pageSize}}).then(res => {
          this.tableData = res.data.books
          this.totalCount = res.data.totalCount
        })
      },
      handleDelete (index, row) {
        console.log(index, row.id)
        this.$axios.delete('/api/books/' + row.id).then(res => {
            this.$message({
              type: 'success',
              message: '删除信息成功'
            })
            this.getUserInfo()    //删除数据，更新视图
          }
        ).catch(resp => {
          console.log(resp)
        })
      },
      handlerAdd() {
        this.dialogAdd.show = true
      },
      handleEdit(index, row) {
        this.dialogEdit.show = true
        this.editForm = row
      },
      //每页显示数据量变更
      handleSizeChange: function(val) {
        this.pageSize = val;
        this.getUserInfo()
      },
      //页码变更
      handleCurrentChange: function(val) {
        this.pageNum = val;
        this.getUserInfo()
      }
    },
    created () {
      this.getUserInfo()
    },
    components: {
      AddUser,
      EditUser
    }
  }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
  h1 {
    font-size: 30px;
    color: #333;
    text-align: center;
    margin: 0 auto;
    padding-bottom: 5px;
    border-bottom: 2px solid #409EFF;
    width: 300px
  }
</style>
